home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 10 - 1994 / 10.12 Dec 94 / GettingStarted / AEHandler.c next >
Encoding:
C/C++ Source or Header  |  1994-09-29  |  9.9 KB  |  495 lines  |  [TEXT/KAHL]

  1. /****************************************/
  2. /*  "AEHandler" source code                */
  3. /*    Copyright 1994, Dave Mark.            */
  4. /*    This code is free to one and all...    */
  5. /****************************************/
  6.  
  7.  
  8. #include <GestaltEqu.h>
  9. #include <AppleEvents.h>
  10.  
  11.  
  12. #define kBaseResID            128
  13. #define kErrorALRTid        128
  14. #define kWINDResID            128
  15.  
  16. #define kVisible            true
  17. #define    kMoveToFront        (WindowPtr)-1L
  18. #define kSleep                60L
  19. #define kNilFilterProc        0L
  20. #define kGestaltMask        1L
  21. #define kKeepInSamePlane    false
  22.  
  23. #define kWindowStartX        20
  24. #define kWindowStartY        50
  25.  
  26. #define mApple                kBaseResID
  27. #define iAbout                1
  28.  
  29. #define mFile                kBaseResID+1
  30. #define iClose                1
  31. #define iQuit                3
  32.  
  33.  
  34. /*************/
  35. /*  Globals  */
  36. /*************/
  37.  
  38. Boolean        gDone;
  39. short        gNewWindowX = kWindowStartX,
  40.             gNewWindowY = kWindowStartY;
  41.  
  42.  
  43. /***************/
  44. /*  Functions  */
  45. /***************/
  46.  
  47. void        ToolboxInit( void );
  48. void        MenuBarInit( void );
  49. void        AEInit( void );
  50. void        AEInstallHandlers( void );
  51. pascal OSErr        DoOpenApp( AppleEvent *event, AppleEvent *reply, long refcon );
  52. pascal OSErr        DoOpenDoc( AppleEvent *event, AppleEvent *reply, long refcon );
  53. OSErr        CheckForRequiredParams( AppleEvent *event );
  54. pascal OSErr        DoPrintDoc( AppleEvent *event, AppleEvent *reply, long refcon );
  55. pascal OSErr        DoQuitApp( AppleEvent *event, AppleEvent *reply, long refcon );
  56. void        OpenDocument( FSSpec *fileSpecPtr );
  57. WindowPtr    CreateWindow( Str255 name );
  58. void        DoCloseWindow( WindowPtr window );
  59. void        EventLoop( void );
  60. void        DoEvent( EventRecord *eventPtr );
  61. void        HandleMouseDown( EventRecord *eventPtr );
  62. void        HandleMenuChoice( long menuChoice );
  63. void        HandleAppleChoice( short item );
  64. void        HandleFileChoice( short item );
  65. void        DoUpdate( EventRecord *eventPtr );
  66. void        DoError( Str255 errorString );
  67.  
  68.  
  69. /******************************** main *********/
  70.  
  71. void    main( void )
  72. {
  73.     ToolboxInit();
  74.     MenuBarInit();
  75.     AEInit();
  76.     
  77.     EventLoop();
  78. }
  79.  
  80.  
  81. /*********************************** ToolboxInit */
  82.  
  83. void    ToolboxInit( void )
  84. {
  85.     InitGraf( &qd.thePort );
  86.     InitFonts();
  87.     InitWindows();
  88.     InitMenus();
  89.     TEInit();
  90.     InitDialogs( 0L );
  91.     InitCursor();
  92. }
  93.  
  94.  
  95. /****************** MenuBarInit ***********************/
  96.  
  97. void    MenuBarInit( void )
  98. {
  99.     Handle            menuBar;
  100.     MenuHandle        menu;
  101.     
  102.     menuBar = GetNewMBar( kBaseResID );
  103.     
  104.     if ( menuBar == NULL )
  105.         DoError( "\pCouldn't load the MBAR resource..." );
  106.     
  107.     SetMenuBar( menuBar );
  108.  
  109.     menu = GetMHandle( mApple );
  110.     AddResMenu( menu, 'DRVR' );
  111.     
  112.     DrawMenuBar();
  113. }
  114.  
  115.  
  116. /******************************** AEInit *********/
  117.  
  118. void    AEInit( void )
  119. {
  120.     OSErr    err;
  121.     long    feature;
  122.     
  123.     err = Gestalt( gestaltAppleEventsAttr, &feature );
  124.     
  125.     if ( err != noErr )
  126.         DoError( "\pError returned by Gestalt!" );
  127.         
  128.     if ( !( feature & ( kGestaltMask << gestaltAppleEventsPresent ) ) )
  129.         DoError( "\pThis configuration does not support Apple events..." );
  130.     
  131.     AEInstallHandlers();
  132. }
  133.  
  134.  
  135. /******************************** AEInstallHandlers *********/
  136.  
  137. void    AEInstallHandlers( void )
  138. {
  139.     OSErr                err;
  140.     
  141.     err = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication,
  142.                 NewAEEventHandlerProc( DoOpenApp ), 0L, false );
  143.     
  144.     if ( err != noErr )
  145.         DoError( "\pError installing 'oapp' handler..." );
  146.     
  147.     err = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments,
  148.                 NewAEEventHandlerProc( DoOpenDoc ), 0L, false );
  149.     
  150.     if ( err != noErr )
  151.         DoError( "\pError installing 'odoc' handler..." );
  152.         
  153.     err = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments,
  154.                 NewAEEventHandlerProc( DoPrintDoc ), 0L, false );
  155.     
  156.     if ( err != noErr )
  157.         DoError( "\pError installing 'pdoc' handler..." );
  158.         
  159.     err = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication,
  160.                 NewAEEventHandlerProc( DoQuitApp ), 0L, false );
  161.     
  162.     if ( err != noErr )
  163.         DoError( "\pError installing 'quit' handler..." );
  164. }
  165.  
  166.  
  167. /****************** DoOpenApp ***********************/
  168.  
  169. pascal OSErr    DoOpenApp( AppleEvent *event, AppleEvent *reply, long refcon )
  170. {
  171.     OpenDocument( nil );
  172.     
  173.     return noErr;
  174. }
  175.  
  176.  
  177. /****************** DoOpenDoc ***********************/
  178.  
  179. pascal OSErr    DoOpenDoc( AppleEvent *event, AppleEvent *reply, long refcon )
  180. {
  181.     OSErr        err;
  182.     FSSpec        fileSpec;
  183.     long        i, numDocs;
  184.     DescType    returnedType;
  185.     AEKeyword    keywd;
  186.     Size        actualSize;
  187.     AEDescList    docList = { typeNull, nil };
  188.  
  189.     // get the direct parameter--a descriptor list--and put
  190.     // it into docList
  191.     err = AEGetParamDesc( event, keyDirectObject,
  192.                             typeAEList, &docList);
  193.  
  194.     // check for missing required parameters
  195.     err = CheckForRequiredParams( event );
  196.     if ( err )
  197.     {
  198.         // an error occurred:  do the necessary error handling
  199.         err = AEDisposeDesc( &docList );
  200.         return    err;
  201.     }
  202.  
  203.     // count the number of descriptor records in the list
  204.     // should be at least 1 since we got called and no error
  205.     err = AECountItems( &docList, &numDocs );
  206.     
  207.     if ( err )
  208.     {
  209.         // an error occurred:  do the necessary error handling
  210.         err = AEDisposeDesc( &docList );
  211.         return    err;
  212.     }
  213.  
  214.     // now get each descriptor record from the list, coerce
  215.     // the returned data to an FSSpec record, and open the
  216.     // associated file
  217.     for ( i=1; i<=numDocs; i++ )
  218.     {
  219.         err = AEGetNthPtr( &docList, i, typeFSS, &keywd,
  220.                             &returnedType, (Ptr)&fileSpec,
  221.                             sizeof( fileSpec ), &actualSize );
  222.  
  223.         OpenDocument( &fileSpec );
  224.     }
  225.  
  226.     err = AEDisposeDesc( &docList );
  227.  
  228.     return    err;
  229. }
  230.  
  231.  
  232. /****************** CheckForRequiredParams ***********************/
  233.  
  234. OSErr    CheckForRequiredParams( AppleEvent *event )
  235. {
  236.     DescType    returnedType;
  237.     Size        actualSize;
  238.     OSErr        err;
  239.  
  240.     err = AEGetAttributePtr( event, keyMissedKeywordAttr,
  241.                     typeWildCard, &returnedType,
  242.                     nil, 0, &actualSize);
  243.  
  244.     if ( err == errAEDescNotFound )    // you got all the required
  245.                                             //parameters
  246.         return    noErr;
  247.     else
  248.         if ( err == noErr )  // you missed a required parameter
  249.             return    errAEParamMissed;
  250.         else    // the call to AEGetAttributePtr failed
  251.             return    err;
  252. }
  253.  
  254.  
  255. /****************** DoPrintDoc ***********************/
  256.  
  257. pascal OSErr    DoPrintDoc( AppleEvent *event, AppleEvent *reply, long refcon )
  258. {
  259.     return noErr;
  260. }
  261.  
  262.  
  263. /****************** DoQuitApp ***********************/
  264.  
  265. pascal OSErr    DoQuitApp( AppleEvent *event, AppleEvent *reply, long refcon )
  266. {
  267.     SysBeep( 20 );
  268.     gDone = true;
  269.     
  270.     return noErr;
  271. }
  272.  
  273.  
  274. /****************** OpenDocument ***********************/
  275.  
  276. void    OpenDocument( FSSpec *fileSpecPtr )
  277. {
  278.     WindowPtr    window;
  279.     
  280.     if ( fileSpecPtr == nil )
  281.         window = CreateWindow( "\p<Untitled>" );
  282.     else
  283.         window = CreateWindow( fileSpecPtr->name );
  284. }
  285.  
  286.  
  287. /****************** CreateWindow ***********************/
  288.  
  289. WindowPtr    CreateWindow( Str255 name )
  290. {
  291.     WindowPtr    window;
  292.     short        windowWidth, windowHeight;
  293.     
  294.     window = GetNewWindow( kWINDResID, nil, kMoveToFront );
  295.     
  296.     SetWTitle( window, name );
  297.     
  298.     MoveWindow( window, gNewWindowX, gNewWindowY, kKeepInSamePlane );
  299.     
  300.     gNewWindowX += 20;
  301.     windowWidth = window->portRect.right - window->portRect.left;
  302.     
  303.     if ( gNewWindowX + windowWidth > qd.screenBits.bounds.right )
  304.     {
  305.         gNewWindowX = kWindowStartX;
  306.         gNewWindowY = kWindowStartY;
  307.     }
  308.         
  309.     gNewWindowY += 20;
  310.     windowHeight = window->portRect.bottom - window->portRect.top;
  311.     
  312.     if ( gNewWindowY + windowHeight > qd.screenBits.bounds.bottom )
  313.     {
  314.         gNewWindowX = kWindowStartX;
  315.         gNewWindowY = kWindowStartY;
  316.     }
  317.     
  318.     ShowWindow( window );
  319.     SetPort( window );
  320.     
  321.     return window;
  322. }
  323.  
  324.  
  325. /****************** DoCloseWindow ***********************/
  326.  
  327. void    DoCloseWindow( WindowPtr window )
  328. {
  329.     if ( window != nil )
  330.         DisposeWindow( window );
  331. }
  332.  
  333.  
  334. /******************************** EventLoop *********/
  335.  
  336. void    EventLoop( void )
  337. {        
  338.     EventRecord        event;
  339.     
  340.     gDone = false;
  341.     while ( gDone == false )
  342.     {
  343.         if ( WaitNextEvent( everyEvent, &event, kSleep, nil ) )
  344.             DoEvent( &event );
  345.     }
  346. }
  347.  
  348.  
  349. /************************************* DoEvent     */
  350.  
  351. void    DoEvent( EventRecord *eventPtr )
  352. {
  353.     char        theChar;
  354.     
  355.     switch ( eventPtr->what )
  356.     {
  357.         case mouseDown: 
  358.             HandleMouseDown( eventPtr );
  359.             break;
  360.         case keyDown:
  361.         case autoKey:
  362.             theChar = eventPtr->message & charCodeMask;
  363.             if ( (eventPtr->modifiers & cmdKey) != 0 ) 
  364.                 HandleMenuChoice( MenuKey( theChar ) );
  365.             break;
  366.         case updateEvt:
  367.             DoUpdate( eventPtr );
  368.             break;
  369.         case kHighLevelEvent:
  370.             AEProcessAppleEvent( eventPtr );
  371.             break;
  372.     }
  373. }
  374.  
  375.  
  376. /************************************* HandleMouseDown */
  377.  
  378. void    HandleMouseDown( EventRecord *eventPtr )
  379. {
  380.     WindowPtr        window;
  381.     short            thePart;
  382.     long            menuChoice;
  383.     
  384.     thePart = FindWindow( eventPtr->where, &window );
  385.     
  386.     switch ( thePart )
  387.     {
  388.         case inMenuBar:
  389.             menuChoice = MenuSelect( eventPtr->where );
  390.             HandleMenuChoice( menuChoice );
  391.             break;
  392.         case inSysWindow : 
  393.             SystemClick( eventPtr, window );
  394.             break;
  395.         case inGoAway:
  396.             if ( TrackGoAway( window, eventPtr->where ) )
  397.                 DoCloseWindow( window );
  398.             break;
  399.         case inContent:
  400.             SelectWindow( window );
  401.             break;
  402.         case inDrag : 
  403.             DragWindow( window, eventPtr->where, &qd.screenBits.bounds );
  404.             break;
  405.     }
  406. }
  407.  
  408.  
  409. /****************** HandleMenuChoice ***********************/
  410.  
  411. void    HandleMenuChoice( long menuChoice )
  412. {
  413.     short    menu;
  414.     short    item;
  415.     
  416.     if ( menuChoice != 0 )
  417.     {
  418.         menu = HiWord( menuChoice );
  419.         item = LoWord( menuChoice );
  420.         
  421.         switch ( menu )
  422.         {
  423.             case mApple:
  424.                 HandleAppleChoice( item );
  425.                 break;
  426.             case mFile:
  427.                 HandleFileChoice( item );
  428.                 break;
  429.         }
  430.         HiliteMenu( 0 );
  431.     }
  432. }
  433.  
  434.  
  435. /****************** HandleAppleChoice ***********************/
  436.  
  437. void    HandleAppleChoice( short item )
  438. {
  439.     MenuHandle    appleMenu;
  440.     Str255        accName;
  441.     short        accNumber;
  442.     
  443.     switch ( item )
  444.     {
  445.         case iAbout:
  446.             SysBeep( 20 );
  447.             break;
  448.         default:
  449.             appleMenu = GetMHandle( mApple );
  450.             GetItem( appleMenu, item, accName );
  451.             accNumber = OpenDeskAcc( accName );
  452.             break;
  453.     }
  454. }
  455.  
  456.  
  457. /****************** HandleFileChoice ***********************/
  458.  
  459. void    HandleFileChoice( short item )
  460. {
  461.     switch ( item )
  462.     {
  463.         case iClose:
  464.             DoCloseWindow( FrontWindow() );
  465.             break;
  466.         case iQuit:
  467.             gDone = true;
  468.             break;
  469.     }
  470. }
  471.  
  472.  
  473. /************************************* DoUpdate     */
  474.  
  475. void    DoUpdate( EventRecord *eventPtr )
  476. {
  477.     WindowPtr    window;
  478.     
  479.     window = (WindowPtr)eventPtr->message;
  480.     
  481.     BeginUpdate(window);
  482.     EndUpdate(window);
  483. }
  484.  
  485.  
  486. /***************** DoError ********************/
  487.  
  488. void    DoError( Str255 errorString )
  489. {
  490.     ParamText( errorString, "\p", "\p", "\p" );
  491.     
  492.     StopAlert( kErrorALRTid, kNilFilterProc );
  493.     
  494.     ExitToShell();
  495. }